// Author: Prasanna V. Loganathar
// Project: Edi.Android
// 
// Copyright 2014 Launchark. All Rights Reserved.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//  
// Created: 11:50 PM 02-05-2014

namespace Edi.Android
{
    using System;
    using System.Web;

    using global::Android.Webkit;

    internal class WebUi : WebViewClient
    {
        public const string AppHandlerScheme = "edi:";
        public const string UiHandlerScheme = "javascript:";

        private MainActivity mainActivity;

        public WebUi(MainActivity activity)
        {
            mainActivity = activity;
        }

        public override bool ShouldOverrideUrlLoading(WebView view, string url)
        {
            // If the URL is not our the custom scheme, just let the webView load the URL as usual

            if (!url.StartsWith(AppHandlerScheme, StringComparison.Ordinal))
            {
                return false;
            }

            var resources = url.Substring(AppHandlerScheme.Length).Split('?');
            var action = resources[0];
            var parameters = HttpUtility.ParseQueryString(resources[1]);

            // Controller based Url handling shall replace what's below.
            if (action == "act")
            {
                var js = string.Empty;
                view.LoadUrl(UiHandlerScheme + js);
            }

            return true;
        }
    }
}